import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/29
 * Time: 23:35
 */

public class Solution {
    private static int i = 0;
    private static TreeNode createTree(int[] arr) {
        int cur = arr[i];
        TreeNode root = null;
        if (cur != -1) {
            root = new TreeNode(cur);
            i++;
            root.left = createTree(arr);
            root.right = createTree(arr);
        }else {
            i++;
        }
        return root;
    }
    public static void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        preorderTraversal(root, list);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode cur = list.get(i);
            TreeNode prev = list.get(i - 1);
            prev.left = null;
            prev.right = cur;
        }
        System.out.println();
    }
    public static void preorderTraversal(TreeNode root, List<TreeNode> list){
        if (root != null) {
            list.add(root);
            preorderTraversal(root.left, list);
            preorderTraversal(root.right, list);
        }
    }
    public static void main(String[] args) {
        TreeNode root = createTree(new int[]{1, 2, 3, -1,-1,4,-1,-1,5,-1,6,-1,-1});
        flatten(root);
    }
}